{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "226401b1",
   "metadata": {},
   "source": [
    "# week5 课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36ba0612",
   "metadata": {},
   "source": [
    "1.有序的数据结构之列表\n",
    "\n",
    "\n",
    "2.用户输入\n",
    "\n",
    "\n",
    "3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ff28e234",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_01_name = \"sb\"\n",
    "# 思考 如果一个变量存放一个班的名字 该怎么村"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8cd8a252",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '网二_name_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [9]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m 网二_name_list_ \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myb,maoerti,chouerti\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43m网二_name_list\u001b[49m)\n",
      "\u001b[1;31mNameError\u001b[0m: name '网二_name_list' is not defined"
     ]
    }
   ],
   "source": [
    "网二_name_list_ = \"yb,maoerti,chouerti\"\n",
    "print(网二_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbae1d19",
   "metadata": {},
   "source": [
    "# 引入列表概念\n",
    "注释：；list可以放数值和字符 但是通常一起放数值 要摸一起放字符串\n",
    "\n",
    "\n",
    "1.取值：通过索引值取出对应内容\n",
    "2.切片：通过索引值区间取出对应的一些内容\n",
    "3.in可以查看对象是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9a48d730",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5e541b02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['yb', 'maoerti', 'chouerti'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网二_name_list = [\"yb\",\"maoerti\",\"chouerti\"]\n",
    "print(网二_name_list,type(网二_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "acfb196a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'maoerti'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出一位同学的名字\n",
    "网二_name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74dc7c0b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "099909d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看有多少同学\n",
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1a7c8978",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '，' (U+FF0C) (866545262.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [23]\u001b[1;36m\u001b[0m\n\u001b[1;33m    print(\"网二:\"，i)\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '，' (U+FF0C)\n"
     ]
    }
   ],
   "source": [
    "for i in 网二_name_list:\n",
    "    print(\"网二:\"，i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "00a00bc5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#in可以查看数据是否存在\n",
    "\"maoerti\" in 网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "27d0c873",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yb', 'maoerti', 'chouerti']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#关于运算法则(in+切片 取值 len(s) s.count)\n",
    "网二_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42eaefb7",
   "metadata": {},
   "source": [
    "## 2.2列表的方法 对列表数据进行操作\n",
    "1.增2.删3.改4.查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9888d3d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "###append增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1973697e",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list.append(\"皮皮\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2d3fcf1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yb', 'maoerti', 'chouerti', '皮皮']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ee26297e",
   "metadata": {},
   "outputs": [],
   "source": [
    "###insret增加到中间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6f808209",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list.insert(1,\"小象\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a1901495",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yb', '小象', 'maoerti', 'chouerti', '皮皮']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "95c2ec7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "###remove删除值 remove经常和in一起使用 先检查是否存在 在进行remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5124f971",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list.remove('chouerti')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "820fb6f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网二_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7917b183",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yb', '小象', 'maoerti', '皮皮']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "1dd7f7ce",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (1867033663.py, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [39]\u001b[1;36m\u001b[0m\n\u001b[1;33m    网二_name_list.remove(\"chouerti\"):\u001b[0m\n\u001b[1;37m                                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#remove经常和in一起使用 先检查是否存在 在进行remove\n",
    "if \"chouerti\" in 网二_name_list:\n",
    "    网二_name_list.remove(\"chouerti\"):\n",
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86a87f0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pop不指定的话删除最后一个，也可指定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "af205b5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网二_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "99fb1f33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'皮皮'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f1f68a54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yb', '小象', 'maoerti']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a1462a76",
   "metadata": {},
   "outputs": [],
   "source": [
    "##移除19的元素\n",
    "age_list = [18,19,18,19,20,25,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12553914",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "470980e2",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '：' (U+FF1A) (2007200387.py, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [56]\u001b[1;36m\u001b[0m\n\u001b[1;33m    for i in range(删除的数量)：\u001b[0m\n\u001b[1;37m                         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '：' (U+FF1A)\n"
     ]
    }
   ],
   "source": [
    "#思路一 remove 先决条件 知道值出现的次数\n",
    "删除的数量=age_list.count(19)\n",
    "for i in range(删除的数量)：\n",
    "    if 19 in age_list:\n",
    "    age_list.remove(19)\n",
    "age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "bca797e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "18244a1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "e5de3d19",
   "metadata": {},
   "outputs": [],
   "source": [
    "#思路二 pop先决条件 知道出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v ==19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "79dc716c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4300694",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
